package com.zj.test;

/**
 * @program: algorithm
 * @description:
 * @author: Zhang Bo
 * @create: 2022-01-25 16:46
 **/
public class Five {


    static class Test {
        volatile long a;
        long v1, v2, v3, v4, v5, v6, v7;
        volatile long b;
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            testCacheLine(new Test());
        }

    }

    private static void testCacheLine(Test test) throws InterruptedException {
        long start = System.currentTimeMillis();

//        Thread t1 = new Thread(() -> {
//            for (int i = 0; i < 100000000; i++) {
//                test.a++;
//            }
//        });


        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100000000; i++) {
                    test.a++;
                }
            }
        });



        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100000000; i++) {
                    test.b++;
                }
            }
        });


//        Thread t2 = new Thread(() -> {
//            for (int i = 0; i < 100000000; i++) {
//                test.b++;
//            }
//        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("总耗时：" + (System.currentTimeMillis() - start) + " ms");
    }
}
